{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "12b260b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c27af5cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.e0745e18417df5f56aac8380e6525386.315360000.1964435415.282335-25904777', 'expires_in': 2592000, 'session_key': '9mzdX+Lh9SrfX1wSvYIhABYUQFyqD8O7w0Y7NO4fjmxHcRPaEwpjiebWnv9QRm/kYmkYsZeUFTUEwhZmdBtG9Bogz7iFxw==', 'access_token': '24.d894092901add42da55e72369b3b6df5.2592000.1651667415.282335-25904777', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'fc7ee6dcb6615b154f23114282d3bcc9'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=AblvqrCFNWG0IcGTVZwkTYOi&client_secret=e5MRWHNpOtoVicw1DrDjuTA3AGD2wB0V'\n",
    "#准备URL参数\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'AblvqrCFNWG0IcGTVZwkTYOi',\n",
    "    'client_secret':'e5MRWHNpOtoVicw1DrDjuTA3AGD2wB0V'\n",
    "}\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "12d4ab92",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'response' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-fed0d38622d6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlihaha_AT\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'access_token'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'response' is not defined"
     ]
    }
   ],
   "source": [
    "lihaha_AT = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "09a34aec",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.d894092901add42da55e72369b3b6df5.2592000.1651667415.282335-25904777'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef0211de",
   "metadata": {},
   "source": [
    "## 通用物体和场景识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "624eac52",
   "metadata": {},
   "source": [
    "* 接口描述\n",
    "> 该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。\n",
    "\n",
    "* HTTP \n",
    "> 方法：POST\n",
    "\n",
    "* 请求URL\n",
    "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "f1dba2cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "89cd8034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>baike_num</td>\n",
       "      <td>否</td>\n",
       "      <td>integer</td>\n",
       "      <td>-</td>\n",
       "      <td>返回百科信息的结果数，默认不返回</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          参数       是否必选       类型 可选值范围  \\\n",
       "0      image    和url二选一   string     -   \n",
       "1        url  和image二选一   string     -   \n",
       "2  baike_num          否  integer     -   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       "2                                   返回百科信息的结果数，默认不返回  "
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html('https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "aa130ca8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '桥梁', 'score': 0.840787, 'root': '建筑-现代建筑', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%A1%A5%E6%A2%81/2054921', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/ae51f3deb48f8c54c5bbc8c630292df5e1fe7fea', 'description': '桥梁，一般指架设在江河湖海上，使车辆行人等能顺利通行的构筑物。为适应现代高速发展的交通行业，桥梁亦引申为跨越山涧、不良地质或满足其他交通需要而架设的使通行更加便捷的建筑物。桥梁一般由上部构造、下部结构、支座和附属构造物组成，上部结构又称桥跨结构，是跨越障碍的主要结构；下部结构包括桥台、桥墩和基础；支座为桥跨结构与桥墩或桥台的支承处所设置的传力装置；附属构造物则指桥头搭板、锥形护坡、护岸、导流工程等。'}}, {'keyword': '天空', 'score': 0.568022, 'root': '自然风景-天空', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%A4%A9%E7%A9%BA/5534', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/241f95cad1c8a7868fa019076909c93d70cf5034', 'description': '天空，汉语词汇，拼音tiān kōng，谓天际空阔；日月星辰罗列的广大空间。是地球重要的组成部分，在古代中国有天圆地方的说法。人可以在天空观测气象或天文现象，从而得知天气变化、时间的流逝或自己的方位。日出日落可知一日中的时间，晚上月亮的盈亏可以知道一个月的时间。北斗星可以指示北方。云的厚度和形状可以知道会否下雨。 在天空可以欣赏到许多美丽的现象，如彩虹、极光和流星雨等。雀鸟会在天空飞翔。 由于石油等化石性燃料使用的增加而产生的悬浮质，特别是那些会在燃烧后释放二氧化硫的煤等燃料的影响，自1973年以来，除了欧洲，天空的能见度正在逐步降低。'}}, {'keyword': '海洋', 'score': 0.38826, 'root': '自然风景-海洋', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%B5%B7%E6%B4%8B/523', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/77094b36acaf2eddba6728be871001e9380193d6', 'description': '海洋(sea)，地理名词，是地球上最广阔的水体的总称。地球表面被各大陆地分隔为彼此相通的广大水域称为海洋，海洋的中心部分称作洋，边缘部分称作海，彼此沟通组成统一的水体。地球上海洋总面积约为3.6亿平方千米，约占地球表面积的71%，平均水深约3795米。海洋中含有十三亿五千多万立方千米的水，约占地球上总水量的97%，而可用于人类饮用只占2%。地球四个主要的大洋为太平洋、大西洋、印度洋、北冰洋，大部分以陆地和海底地形线为界。当今人类已探索的海底只有5%，还有95%大海的海底是未知的。'}}, {'keyword': '屏幕截图', 'score': 0.199602, 'root': '非自然图像-屏幕截图', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE/3634161', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/aa18972bd40735fa016882639c510fb30e240854', 'description': '所谓屏幕截图就是将电脑屏幕上的桌面、窗口、对话框、选项卡等屏幕元素保存为图片。在Windows下用户可以使用键盘上的“打印屏幕系统请求”(Print Screen)按键进行整个屏幕的截图和当前活动窗口的截图(按住Alt键的同时按下Print Screen键)，还可以借助专业的屏幕截图软件进行截图。'}}, {'keyword': '夜景照明', 'score': 0.01382, 'root': '建筑-建筑夜景', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%A4%9C%E6%99%AF%E7%85%A7%E6%98%8E/7167225', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/902397dda144ad345446e430daa20cf430ad858f', 'description': '夜景照明(nightscape lighting) 泛指除功能性照明之外，针对性美化夜间人文景观，自然景观的照明。照明的对象有建筑或构筑物，广场、道路和桥梁，机场、车站和码头，名胜古迹，园林绿地，山体，江河水面，商业街和广告标志以及城市市政设施等的照明，其目的就是利用灯光将上述照明对象加以重塑，并有机地组合成一个和谐协调、优美壮观和富有特色的夜景图画，以此来表现一个城市或地区的夜间形象。'}}], 'log_id': 1510960479426570660}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('02.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload={\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "6bfe2e9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'calorie': '318', 'has_calorie': True, 'name': '糖醋排骨', 'probability': '0.518581', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%B3%96%E9%86%8B%E6%8E%92%E9%AA%A8/31447', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/562c11dfa9ec8a1391024834f903918fa1ecc0ce', 'description': '糖醋排骨(Sweet and Sour Spare Ribs)，是糖醋味型中具有代表性的一道大众喜爱的特色传统名菜。它选用新鲜猪子排作主料，肉质鲜嫩，成菜色泽红亮油润。'}}, {'calorie': '278', 'has_calorie': True, 'name': '排骨汤', 'probability': '0.193673', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%8E%92%E9%AA%A8%E6%B1%A4/2637533', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/5ab5c9ea15ce36d3c93f85e330f33a87e950b12d', 'description': '排骨汤，是用猪、牛、羊等动物剔肉后剩下的肋骨和脊椎骨制作的一道家常汤品。排骨除含蛋白、脂肪、维生素外，还含有大量磷酸钙、骨胶原、骨粘蛋白等，可为幼儿和老人提供钙质。'}}, {'calorie': '54', 'has_calorie': True, 'name': '鸡脆骨', 'probability': '0.0481623', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%B8%A1%E8%84%86%E9%AA%A8/8260113', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/b7fd5266d01609242782dfabde0735fae7cd34de', 'description': '鸡脆骨，又称掌中宝、鸡脆。它以其独特的口感而倍受食客青睐，常见的菜例有宫保鸡脆骨、椒盐鸡脆，麻辣鸡脆骨、燕山鸡脆。对于不同的鸡脆骨菜肴，往往需要采取不同的腌制方法，方能达到独特的成菜效果。而且口感清脆，和人口味，颇受很多人青睐。'}}, {'calorie': '260', 'has_calorie': True, 'name': '蜜汁小排', 'probability': '0.01983', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%9C%9C%E6%B1%81%E5%B0%8F%E6%8E%92/9635303', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/c995d143ad4bd11373f002810ee6b30f4bfbfbedaf65', 'description': '蜜汁小排是用小排骨1斤，蜂蜜2大匙，糖3大匙，酱油1大匙，盐1小匙，姜3大片，料酒3小匙制作的美食。'}}, {'calorie': '80', 'has_calorie': True, 'name': '骨肉相连', 'probability': '0.0171292', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%AA%A8%E8%82%89%E7%9B%B8%E8%BF%9E/10318735', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/962bd40735fae6cdb416980e01b30f2442a70f3a', 'description': '骨肉相连是一道以鸡腿肉为主要原料的菜品。'}}], 'log_id': 1510960211073786662}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('03.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "\n",
    "payload={\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71242ecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 3.请看成品\n",
    "微信小程序搜索：识相Insight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1d9bc1a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
